<template>
  <div class="table-box">
    <div class="table-search">
      <div class="table-search">
        <el-form :inline="true" :model="form">
          <el-form-item label="渠道名称">
            <el-input v-model="form.channel" placeholder="渠道名称" />
          </el-form-item>
          <el-form-item label="项目名称">
            <el-input v-model="form.project" placeholder="项目名称" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onQuery">查询</el-button>
            <el-button type="info" @click="onReset">重置</el-button>
            <el-button type="danger" @click="onAdd">新增</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!-- <div class="btn-box">
        <el-button type="primary" @click="onAdd">新增</el-button>
        <el-button type="primary" plain @click="onExport">导出</el-button>
      </div> -->
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column prop="channel" label="渠道" align="center" />
          <el-table-column prop="project" label="项目名称" align="center" />
          <el-table-column prop="version" label="版本" align="center" />
          <el-table-column
            prop="version_code"
            label="版本Code"
            align="center"
          />
          <el-table-column
            prop="version_log"
            label="更新提示语"
            align="center"
          />
          <el-table-column prop="is_force" label="是否强更" align="center">
            <template #default="scope">
              <span>{{ scope.row.is_force == 1 ? "是" : "否" }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="size" label="包大小" align="center" />
          <el-table-column
            prop="upgrade_url"
            label="包下载地址"
            align="center"
            width="180"
          />
          <el-table-column prop="min_code" label="最新Code" align="center" />
          <el-table-column prop="max_code" label="最大Code" align="center" />
          <el-table-column
            prop="update_date"
            label="更新日期"
            align="center"
            :formatter="formatterTime"
          />
          <!-- <el-table-column prop="createtime" label="创建时间" align="center" />> -->

          <el-table-column
            label="操作"
            align="center"
            fixed="right"
            width="120"
          >
            <template #default="scope">
              <el-button
                type="primary"
                plain
                @click="handleOperate(scope.$index, scope.row, 'edit')"
                >更新</el-button
              >
              <!-- <el-button
                type="danger"
                @click="handleOperate(scope.$index, scope.row, 'delete')"
                >删除</el-button
              > -->
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            v-model:current-page="pageable.pageNum"
            v-model:page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      :dialogFormVisible="dialogVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
    />
  </div>
</template>
<script setup>
import { ref, onMounted, reactive } from "vue";
import { exportToExcel, formatterTime } from "@/utils/utils";
import { appversionListApi, appversionAddApi } from "@/api/payServerse/upgrade";
import DialogForm from "./components/dialogForm.vue";
const loading = ref(false);
const tableData = ref([]);
const btnLoading = ref(false);
const dialogVisible = ref(false);
const btnType = ref(null);
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});
const formatIsRequired = (row) => {
  return row.is_required === 1 ? "是" : "否";
};

const form = reactive({
  channel: "",
  project: "",
});
const dialogFormData = {
  channel: "",
  project: "",
  version: "",
  version_code: "",
  version_log: "",
  update_date: "",
  size: "",
  upgrade_url: "",
  id: "",
  min_code: "",
  max_code: "",
  is_force: "",
};
const dialogForm = reactive({ ...dialogFormData });
// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pageSize: pageable.pageSize,
    channel: form.channel,
    project: form.project,
  };
};

// 查询
const onQuery = () => {
  getTbaleData();
};

// 查询
const onReset = () => {
  form.channel = "";
  form.project = "";
  getTbaleData();
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await appversionListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count || 0;
  } catch (error) {
    loading.value = false;
  }
};

// 查询
const onAdd = async () => {
  dialogVisible.value = true;
  btnType.value = "add";
};
//关闭
const closeDialog = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, dialogFormData);
};

// 取消
const cancel = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, dialogFormData);
};
// 提交
const submit = async (param) => {
  try {
    btnLoading.value = true;
    if (btnType.value == "add") {
      delete param.id;
      const res = await appversionAddApi(param);
      ElMessage({
        message: res.message,
        type: "success",
      });
    }
    if (btnType.value == "edit") {
      console.log(param, "param");
      const res = await appversionAddApi(param);
      ElMessage({
        message: res.message,
        type: "success",
      });
    }
    dialogVisible.value = false;
    btnLoading.value = false;
    getTbaleData();
  } catch (error) {
    btnLoading.value = false;
    dialogVisible.value = false;
  }
};
const handleOperate = async (index, row, type) => {
  // 编辑
  if (type === "edit") {
    btnType.value = "edit";
    dialogVisible.value = true;
    Object.assign(dialogForm, { ...row });
  }
  // 删除
  if (type === "delete") {
    ElMessageBox.confirm("您是否确认删除此商品?", "温馨提示", {
      type: "warning",
    }).then(async () => {
      const res = await appversionDeleteApi({ id: row.id });
      getTbaleData();
      ElMessage.success(res.message);
    });
  }
};
// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
